var $ = require('../../utils/Ajax.js');
Page({
  data:{
    BankState: 0,
    // BankState参数为 0 时显示添加银行卡，不为 0 时显示已经添加的银行卡
    BankInfor: null,
    // 图片地址
    PictureUrl: getApp().PictureUrl,
    Id: null,
    Maney: null,
    UserManey: null
  },
  onShow: function(){
    this.setData({
      UserManey: wx.getStorageSync('UserManey')
    })
    var that = this;
    $.Ajax("api/x_user_card", "POST", {}, function (res) {
      that.setData({
        BankInfor: res.d.userCard,
        BankState: res.d.userCard.length
      })
      for (var i = 0; i < that.data.BankInfor.length;i++){
        var number = that.data.BankInfor[i].number;
        var numberL = number.substr(0,3);
        var numberR = number.substr(number.length - 4);
        var BankInfor = `BankInfor[${i}].number`;
        that.setData({
          [BankInfor]: numberL + ' **** **** ' + numberR
        })
      }
    });
  },
  onLoad: function(){
    wx.setNavigationBarTitle({
      title: "提现"
    }) 
  },
  // 添加银行卡
  AddBank: function(){
    wx.navigateTo({
      url: '../AddBank/AddBank',
    })
  },

  // 再次添加银行卡
  NewAddBank: function(){
    wx.navigateTo({
      url: '../AddBank/AddBank',
    })
  },

  // 选择提现的银行卡
  radioChange: function(e){
    this.setData({
      Id: e.detail.value
    })
  },
  // 提现金额
  Maney: function(e){
    this.setData({
      Maney: e.detail.value
    })
  },
  // 全部提现
  Extract_: function(){
    this.setData({
      Maney: wx.getStorageSync('UserManey')
    })
  },
  // 确认提现
  Extract: function () {
    var that = this;
    var Maney = Number(this.data.Maney)
    var NumBol = /^[0-9]*[0-9]$/i;
    if (that.data.Id == null){
      wx.showToast({
        title: '请选择银行卡',
        icon: 'success',
        image: '../../images/err.png',
        duration: 2000
      })
    }else if(!(NumBol.test(Maney))){
      wx.showToast({
        title: '非法字符',
        icon: 'success',
        image: '../../images/err.png',
        duration: 2000
      })
    }else if(Maney % 10 != 0) {
      wx.showToast({
        title: '请输入10的倍数',
        icon: 'success',
        image: '../../images/err.png',
        duration: 2000
      })
    } else if (Maney <= 1){
      wx.showToast({
        title: '提现金额有误',
        icon: 'success',
        image: '../../images/err.png',
        duration: 2000
      })
    }else{
      $.Ajax("api/x_cash", "POST", {
        cid: that.data.Id,
        money: that.data.Maney
      }, function (res) {
        if (res.s){
          wx.showToast({
            title: res.m,
            icon: 'success',
            duration: 1500
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 2
            })
          },2000)
        }else{
          wx.showToast({
            title: res.m,
            icon: 'success',
            image: '../../images/err.png',
            duration: 1500
          })
        }
      });
    }
  }
})